package com.ilink.teacherservice.controller.videoTranscription;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;

public class TranscriptionPhrasesManager {

    // 公共配置
    private static final String DOMAIN = "tingwu.cn-beijing.aliyuncs.com";
    private static final String VERSION = "2023-09-30";
    private static final ProtocolType PROTOCOL_TYPE = ProtocolType.HTTPS;
    private static final DefaultProfile PROFILE = DefaultProfile.getProfile("cn-beijing", "LTAI5tLm9VNHqQG2w6p2ceJj", "TxZMYPzxh5krlNwSlMmLdJiDdQ8Uok");
    private static final IAcsClient CLIENT = new DefaultAcsClient(PROFILE);

    // 创建热词词表
    public static void createTranscriptionPhrases() throws ClientException {
        CommonRequest request = createCommonRequest(MethodType.POST, "/openapi/tingwu/v2/resources/phrases");

        JSONObject root = new JSONObject();
        root.put("Name", "X大模型创新训练营第一期");
        root.put("Description", "X大模型创新训练营第一期课程");

        JSONObject wordWeights = new JSONObject();
        wordWeights.fluentPut("鄂海红", 3).fluentPut("雏雁计划", 3);
        root.put("WordWeights", wordWeights);

        request.setHttpContent(root.toJSONString().getBytes(), "utf-8", FormatType.JSON);
        CommonResponse response = CLIENT.getCommonResponse(request);
        System.out.println(response.getData());
    }

    // 查询热词词表
    public static void getTranscriptionPhrases(String phraseId) throws ClientException {
        CommonRequest request = createCommonRequest(MethodType.GET, String.format("/openapi/tingwu/v2/resources/phrases/%s", phraseId));
        CommonResponse response = CLIENT.getCommonResponse(request);
        System.out.println(response.getData());
    }

    // 更新热词词表
    public static void updateTranscriptionPhrases(String phraseId) throws ClientException {
        CommonRequest request = createCommonRequest(MethodType.PUT, String.format("/openapi/tingwu/v2/resources/phrases/%s", phraseId));

        JSONObject root = new JSONObject();
        root.put("Name", "X大模型创新训练营第一期");
        root.put("Description", "X大模型创新训练营第一期课程");

        JSONObject wordWeights = new JSONObject();
        wordWeights.fluentPut("鄂海红", 2)
            .fluentPut("鄂老师",2)
            .fluentPut("汤子辰",2)
            .fluentPut("并发量",2)
            .fluentPut("北京邮电大学", 2)
            .fluentPut("雏雁计划", 2)
            .fluentPut("大模型创新训练营", 2)
            .fluentPut("智能体", 2)
            .fluentPut("LangChain", 2)
            .fluentPut("通义千问", 2)
            .fluentPut("编排", 2)
            .fluentPut("科大讯飞", 2)
            .fluentPut("讯飞星火", 2)
            .fluentPut("Agent", 2);
        root.put("WordWeights", wordWeights);

        request.setHttpContent(root.toJSONString().getBytes(), "utf-8", FormatType.JSON);
        CommonResponse response = CLIENT.getCommonResponse(request);
        System.out.println(response.getData());
    }

    // 删除热词词表
    public static void deleteTranscriptionPhrases(String phraseId) throws ClientException {
        CommonRequest request = createCommonRequest(MethodType.DELETE, String.format("/openapi/tingwu/v2/resources/phrases/%s", phraseId));
        CommonResponse response = CLIENT.getCommonResponse(request);
        System.out.println(response.getData());
    }

    // 列举热词词表
    public static void listTranscriptionPhrases() throws ClientException {
        CommonRequest request = createCommonRequest(MethodType.GET, "/openapi/tingwu/v2/resources/phrases");
        CommonResponse response = CLIENT.getCommonResponse(request);
        System.out.println(response.getData());
    }

    // 公共的请求创建方法
    private static CommonRequest createCommonRequest(MethodType method, String uri) {
        CommonRequest request = new CommonRequest();
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysProtocol(PROTOCOL_TYPE);
        request.setSysMethod(method);
        request.setSysUriPattern(uri);
        request.setHttpContentType(FormatType.JSON);
        return request;
    }

    // main函数调用五个函数
    public static void main(String[] args) throws ClientException {
        //createTranscriptionPhrases();
        updateTranscriptionPhrases("ef33f45864c34f7281252332224c3ba9");
        getTranscriptionPhrases("ef33f45864c34f7281252332224c3ba9");
        //deleteTranscriptionPhrases("ef33f45864c34f7281252332224c3ba9");
        //listTranscriptionPhrases();
    }
}
